import {
  resourceTypeList,
  resourceTypeInsert,
  resourceTypeDel,
  resourceTypeEdit
} from '../services/resource'
import { message } from "antd";

export default {
  namespace: 'resourceType',

  state: {
    addForm: {},
    searchForm: {},
    modalForm: {},
    list: [],
    selection: {},
    modalVisible: false,
    optFlag: '',
    resTypeOptions: []
  },

  effects: {
    * page({payload}, {call, put}) {
      const response = yield call(resourceTypeList, payload);
      if (response && response.code === 0) {
        const list = response.result;
        yield put({
          type: 'queryList',
          payload: list,
        });
      }

    },

    * insert({payload, callback}, {call, put}) {
      const {fields, searchForm} = payload;
      const response = yield call(resourceTypeInsert, fields);
      if (response && response.code === 0) {
        message.success('添加成功');
        yield put({
          type: "page",
          payload: {...searchForm}
        });
        yield put({
          type: 'handleModalVisible',
          payload: {modalVisible: false}
        });
        yield put({
          type: "handleCleanAddForm",
          payload: {}
        });
        yield put({
          type: 'getOptions'
        });
      }
    },

    * del({payload, callback}, {call, put}) {
      const {fields, searchForm} = payload;
      const response = yield call(resourceTypeDel, fields);
      if(response && response.code === 0){
        message.success('删除成功');
        yield put({
          type: 'page',
          payload: {...searchForm}
        });
        yield put({
          type: "handleTableSelection",
          payload: {}
        });
        yield put({
          type: 'handleModalVisible',
          payload: {modalVisible: false}
        });
        yield put({
          type: 'getOptions'
        });
      }
    },

    * update({payload, callback}, {call, put}) {
      const {fields, searchForm} = payload;
      const response = yield call(resourceTypeEdit, fields);
      if(response && response.code === 0){
        message.success('修改成功');
        yield put({
          type: "page",
          payload: {...searchForm}
        });
        yield put({
          type: "handleTableSelection",
          payload: fields
        });
        yield put({
          type: 'handleModalVisible',
          payload: {modalVisible: false}
        });
        yield put({
          type: 'getOptions'
        });
      }
    },

    * getOptions({},{call, put}){
      const response = yield call(resourceTypeList);
      if (response && response.code === 0) {
        const list = response.result;
        yield put({
          type: 'putOptions',
          payload: list,
        });
      }
    }
  },

  reducers: {
    queryList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    handleSearchChange(state, action) {
      const {searchForm} = state;
      const newSearchForm = {
        ...searchForm,
        ...action.payload
      }
      return {
        ...state,
        searchForm: newSearchForm
      }
    },
    handleModalVisible(state, action) {
      return {
        ...state,
        ...action.payload
      }
    },

    handleTableSelection(state, action) {
      return {
        ...state,
        selection: action.payload
      }
    },

    handleModalChange(state, action) {
      const {modalForm} = state;
      const {fields, optFlag} = action.payload;
      const newModalForm = {
        ...modalForm,
        ...fields
      }
      let st = {
        ...state,
        modalForm: newModalForm
      }
      if(optFlag === "add"){
        let { addForm }= action.payload
        Object.keys(fields).map(key => {
          addForm[key] = fields[key]['value'];
        })
        st = {
          ...st,
          addForm: addForm
        }
      }
      return st
    },
    handleCleanAddForm(state, action) {
      return {
        ...state,
        addForm: action.payload
      }
    },
    putOptions(state, action) {
      const types = action.payload;
      let options = [];
      types.map(item => {
        let option = {};
        option.value = item.code;
        option.label = item.name;
        options.push(option);
      })
      return {
        ...state,
        resTypeOptions: options
      }
    }
  },


}
